﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TemplatetoSQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 打开数据库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sqlconn sl = new sqlconn();
            sl.Show();
        }
        public static String conng
        {
            set;
            get;
        }
        public static String databasename
        {
            set;
            get;
        }
        public static string tablename
        {
            set;
            get;
        }
        public void showdata(string conns,string db)
        {
            conng = conns;
            databasename = db;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = null;
            MySqlCommand command = null;
            MySqlDataAdapter adap = null;
            DataTable ds = new DataTable();
            Form1 fo1 = new Form1();
            try
            {
                if (conng == null) { MessageBox.Show("请添加数据库"); return; }
                conn = new MySqlConnection(conng);
                command = conn.CreateCommand();
                command.CommandText = "show tables";
                conn.Open();
                adap = new MySqlDataAdapter(command);
                adap.Fill(ds);
                conn.Close();
            }
            catch (Exception en)
            {

                Console.WriteLine(en);
                MessageBox.Show("请重新连接数据库");

            }
            for (int i = 0; i < ds.Rows.Count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = ds.Rows[i].ItemArray[0].ToString();
                treeView1.Nodes.Add(tn);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string tv1 = treeView1.SelectedNode.Text;
            tablename = tv1;
            MySqlConnection conn = null;
            MySqlCommand commands = null;
            MySqlDataAdapter adap = null;
            DataTable ng = new DataTable();
            try
            {
                
                if (conng == null) { MessageBox.Show("请添加数据库"); return; }
                conn = new MySqlConnection(conng);
                commands = conn.CreateCommand();
                commands.CommandText = "SELECT * FROM " + tv1;
                conn.Open();
                adap = new MySqlDataAdapter(commands);
                adap.Fill(ng);
                conn.Close();
                string[] listname = new string[ng.Columns.Count];
                for (int i = 0; i < ng.Columns.Count; i++)
                {
                    listname[i] += ng.Columns[i].ColumnName + "$" + ng.Columns[i].DataType.Name;
                }
                for (int i = 0; i < listname.Length; i++)
                {
                    string[] fenli = listname[i].Split('$');
                    string strname = fenli[0];
                    string strtype = fenli[1];
                    int index = this.dataGridView1.Rows.Add();
                    this.dataGridView1.Rows[index].Cells[0].Value = strname;
                    this.dataGridView1.Rows[index].Cells[1].Value = strtype;
                }
            }
            catch (Exception en)
            {
                MessageBox.Show("数据表出错，请检查数据");
                throw;
            }
        }
        public class dataview{
            public static string dn { set; get; }
            public static string dt { set; get; }
            public static string dtf { set; get; }
        }
        public void getdatagridview()
        {
            Form1 f1=new Form1();
            int datacount = f1.dataGridView1.RowCount;
            List<dataview> dv1 = new List<dataview>();
            for (int i = 0; i < datacount; i++)
            {
                //dv1.dn.Add(f1.dataGridView1[i][1]);
            }
        }

        public void temp1(string name,string type)
        {
            //Environment.NewLine
            switch (type)
            {
                case "int32":
                    break;
                default:
                    break;
            }
            string maintemp1 = "";
        }



        public void savefile()
        {
            string header = "package com." + databasename + ".model;" + Environment.NewLine +
                        "import java.io.Serializable;" + Environment.NewLine +
                        "import java.util.Date;" + Environment.NewLine +
                        "import com.baomidou.mybatisplus.annotations.TableField;" + Environment.NewLine +
                        "import com.baomidou.mybatisplus.annotations.TableId;" + Environment.NewLine +
                        "import com.baomidou.mybatisplus.enums.IdType;" + Environment.NewLine +
                        "import com.iot.commons.utils.JsonUtils;";
            string main1 = "public class " + tablename + " implements Serializable {" + Environment.NewLine;


            SaveFileDialog objsave = new SaveFileDialog();
            objsave.Filter = "(*.txt)|*.txt|" + "(*.*)|*.*";
            objsave.FileName = "sss.txt";
            if (objsave.ShowDialog() == DialogResult.OK)
            {
                StreamWriter FileWriter = new StreamWriter(objsave.FileName,true);
                FileWriter.Write(header);//写入文件1
                FileWriter.Write(header);//写入文件2
                FileWriter.Write(header);//写入文件3
                FileWriter.Close();
            }
        }
    }
}
